package net.doglog;

import java.io.*;

import javax.servlet.ServletException;
import javax.servlet.http.*;

@SuppressWarnings("serial")
public class PostAdminNew extends HttpServlet {
	public void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		req.getRequestDispatcher("/admin/post/new.jsp").forward(req, resp);
	}
	
	public void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		String title = req.getParameter("title");
		String content = req.getParameter("content");
		String tags = req.getParameter("tags");
		try {
			Post post = PostUtil.newPost(title, content, tags);
			resp.sendRedirect("/post/" + post.getId());
		} catch (Throwable th) {
			req.setAttribute("title", title);
			req.setAttribute("content", content);
			req.setAttribute("tags", tags);
			req.setAttribute("error", th.getMessage());
			req.getRequestDispatcher("/admin/post/new.jsp").forward(req, resp);
		}
	}
}
